package cmd_cache

import (
	"fmt"
	"gitee.com/theegg/go-brick/app/boot"
	"gitee.com/theegg/go-brick/app/service/app"
	"gitee.com/theegg/go-brick/app/service/help/credis"
	"github.com/spf13/cobra"
)

var (
	dir  string
	sign string
)

var StartCmd = &cobra.Command{
	Use:          "cache",
	Short:        "刷新缓存",
	Example:      "go-brick cache -d /data/www/go-brick",
	SilenceUsage: true,
	RunE: func(cmd *cobra.Command, args []string) error {
		return run()
	},
}

func init() {
	StartCmd.PersistentFlags().StringVarP(&dir, "dir", "d", "", "启动服务，指定根目录,配置文件和storage在此目录")
	StartCmd.PersistentFlags().StringVarP(&sign, "sign", "s", "all", "需要清理缓存的商户")
}

func run() error {
	errBoot := boot.InitApp(dir)
	if errBoot != nil {
		return errBoot
	}

	credis.New().FlushAll()

	fmt.Println("--当前环境为" + app.GetEnv() + ", 刷新Redis缓存完成!")
	return nil
}
